package com.transaction.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {

    public static final String FORMAT_STR_TIME14 = "yyyyMMddHHmmss";
    public static final String FORMAT_STR_TIME17 = "yyyyMMddHHmmssSSS";
    public static final String FORMAT_STR_TIME8 = "yyyyMMdd";
    public static final String FORMAT_STR_TIME10 = "yyyy-MM-dd";
    public static final String FORMAT_STR_TIME10_2 = "yyyy/MM/dd";
    public static final String FORMAT_STR_TIME6 = "HHmmss";
    public static final String FORMAT_STR_TZ = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";

    /**
     * 随机一个出生日期
     * @Title: randomBirthDay
     * @author: dy.yin 2021/8/18 14:06
     */
    public static String randomBirthDay() {
        StringBuilder birthDay = new StringBuilder();
        Integer year = 1921 + new Random().nextInt(90);
        Integer month = new Random().nextInt(12) + 1;
        Integer day = new Random().nextInt(30) + 1;
        if (month == 2) {
            day = 28;
        }

        birthDay.append(year);

        if(String.valueOf(month).length() < 2){
            birthDay.append("0").append(month);
        }else{
            birthDay.append(month);
        }

        if(String.valueOf(day).length() < 2){
            birthDay.append("0").append(day);
        }else{
            birthDay.append(day);
        }

        return birthDay.toString();
    }

    //日期格式转换
    public static String formatDate(Date date, String format){
        SimpleDateFormat sf = new SimpleDateFormat(format);
        return sf.format(date);
    }

    /**
     * 字符串转日期
     * @param date 字符串
     * @param format 字符串格式
     * @return
     */
    public static Date strFormatDate(String date, String format){
        SimpleDateFormat sf = new SimpleDateFormat(format);
        try {
            return sf.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 字符串日期格式转换
     * @param date 字符串日期
     * @param in 传入字符串格式
     * @param out 返回字符串格式
     * @return
     */
    public static String strFormatStr(String date, String in,String out){
        SimpleDateFormat sf = new SimpleDateFormat(in);
        try {
            return formatDate(sf.parse(date),out);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 解析两个日期段之间的所有日期
     * @param dateList 日期起始和结束集合
     * @return
     */
    public static List<String> getDayListOfDateByList(List<String> dateList) {
        String beginDateStr = dateList.get(0);
        String endDateStr = dateList.get(1);
        return getDayListOfDateBy2Day(beginDateStr,endDateStr);
    }

    /**
     * 解析两个日期段之间的所有日期
     * @param beginDateStr 起始日期 yyyyMMdd
     * @param endDateStr 结束日期 yyyyMMdd
     * @return
     */
    public static List<String> getDayListOfDateBy2Day(String beginDateStr,String endDateStr) {

        // 指定要解析的时间格式
        SimpleDateFormat f = new SimpleDateFormat(FORMAT_STR_TIME8);

        // 定义一些变量
        Date beginDate = null;
        Date endDate = null;

        Calendar beginGC = null;
        Calendar endGC = null;
        List<String> list = new ArrayList<String>();

        try {
            // 将字符串parse成日期
            beginDate = f.parse(beginDateStr);
            endDate = f.parse(endDateStr);

            // 设置日历
            beginGC = Calendar.getInstance();
            beginGC.setTime(beginDate);

            endGC = Calendar.getInstance();
            endGC.setTime(endDate);
            // 直到两个时间相同
            while (beginGC.getTime().compareTo(endGC.getTime()) <= 0) {

                list.add(f.format(beginGC.getTime()));
                // 以日为单位，增加时间
                beginGC.add(Calendar.DAY_OF_MONTH, 1);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 把字符串日期带T和Z的转换成特定格式的日期字符串
     * @return
     */
    public static String dateStrContainsTAndZToDateStr(String date,String formatter){
        SimpleDateFormat dateFormat = new SimpleDateFormat(FORMAT_STR_TZ, Locale.US);
        try {
            Date retDate = dateFormat.parse(date.replace("Z","+0000"));
            DateFormat dateFormat1 = new SimpleDateFormat(formatter);
            return dateFormat1.format(retDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 传入日期字符串返回周几  1：周日 2：周一 。。。 7：周六
     * @param day 字符串日期 yyyyMMdd
     * @return
     */
    public static int getDayForWeek(String day){
        SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.FORMAT_STR_TIME8);
        Date date = null;
        try {
            date = sdf.parse(day);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_WEEK);
    }

    public static void main(String[] args) {
        String a = dateStrContainsTAndZToDateStr("2022-04-20T16:00:00.000Z","yyyy-MM-dd");
        System.out.println(a);
    }
}
